package top.tangyh.basic.spring_mvc.service.system.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  top.tangyh.basic.spring_mvc.service.SuperServiceImpl;
import  top.tangyh.basic.spring_mvc.database.mybatis.conditions.Wraps;
import  top.tangyh.basic.spring_mvc.database.mybatis.conditions.query.LbQueryWrap;
import  top.tangyh.basic.spring_mvc.util.ArgumentAssert;
import top.tangyh.basic.spring_mvc.entity.BasePosition;
import top.tangyh.basic.spring_mvc.manager.BasePositionManager;
import top.tangyh.basic.spring_mvc.service.system.BasePositionService;
import top.tangyh.basic.spring_mvc.vo.save.system.BasePositionSaveVO;
import top.tangyh.basic.spring_mvc.vo.update.system.BasePositionUpdateVO;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 岗位
 * </p>
 *
 * @author CJ
 * @date 2021-10-18
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)

public class BasePositionServiceImpl extends SuperServiceImpl<BasePositionManager, Long, BasePosition> implements BasePositionService {

    @Override
    public boolean check(String name, Long orgId, Long id) {
        ArgumentAssert.notEmpty(name, "请填写名称");
        LbQueryWrap<BasePosition> wrap = Wraps.<BasePosition>lbQ().eq(BasePosition::getName, name).eq(BasePosition::getOrgId, orgId).ne(BasePosition::getId, id);
        return superManager.count(wrap) > 0;
    }

    @Override
    protected <SaveVO> BasePosition saveBefore(SaveVO saveVO) {
        BasePositionSaveVO positionSaveVO = (BasePositionSaveVO) saveVO;
        ArgumentAssert.isFalse(check(positionSaveVO.getName(), positionSaveVO.getOrgId(), null), StrUtil.format("岗位[{}]已经存在", positionSaveVO.getName()));
        return super.saveBefore(saveVO);
    }

    @Override
    protected <UpdateVO> BasePosition updateBefore(UpdateVO updateVO) {
        BasePositionUpdateVO positionUpdateVO = (BasePositionUpdateVO) updateVO;
        ArgumentAssert.isFalse(check(positionUpdateVO.getName(), positionUpdateVO.getOrgId(), positionUpdateVO.getId()), StrUtil.format("岗位[{}]已经存在", positionUpdateVO.getName()));
        return super.updateBefore(updateVO);
    }

    /**
     *
     * [描述]: 可以自身实现save，也可以继承SuperServiceImpl类，并实现save方法
     * @Autowired
     * public BasePositionManager basePositionManager;
     *
     * save{
     *     Entity entity = saveBefore(saveVO);
     *     basePositionManager.save(entity);
     *     //下钻到basePositionManager 岗位管理层
     *     saveAfter(saveVO, entity);
     *     return entity;
     *}
     */
}
